package user;

import book.Book;
import book.Library;
import book.PairOfUidAndBookId;
import constant.Constant;
import utils.AnalyzingBorrowedBook;
import utils.ScannerSingleton;

import java.io.IOException;
import java.util.Scanner;

public class NormalUser extends User{

    // 用户已借阅的图书相关信息
    private PairOfUidAndBookId[] pairOfUidAndBookIds;

    // 当前书籍的借阅量
    private int borrowedCount;

    // 最多借阅的图书数量
    private static final int BORROW_BOOK_MAX_NUM = 5;

    private final AnalyzingBorrowedBook analyzingBorrowedBook = new AnalyzingBorrowedBook();

    private Library library = null;
    private Scanner scanner = null;

    public NormalUser(String name, int userId){
        super(name, userId, "普通用户");
        loadBorrowedBook();

        library = Library.getInstance();
        scanner = ScannerSingleton.getInstance();

    }

    private void loadBorrowedBook() {
        PairOfUidAndBookId[] allBorrowedBook;
        try {
            //1.先加载文件当中的借阅信息
            allBorrowedBook = analyzingBorrowedBook.loadBorrowedBook(Constant.BORROWED_BOOK_FILE_NAME);

            //2. 默认已借阅的图书数组大小为BORROW_BOOK_MAX_NUM，这里也可以定义到常量
            pairOfUidAndBookIds = new PairOfUidAndBookId[BORROW_BOOK_MAX_NUM];
            // 3. 没有读取到已借阅的图书信息
            if(allBorrowedBook == null){
                borrowedCount = 0;
            }else {
                //4. 查看时机读取到的数组长度是多少?
                int allBorrowedBookLen = allBorrowedBook.length;
                //5. 如果读取到了10本书被借阅, 但是当前borrowedBooks数组长度小于10
                if(allBorrowedBookLen > pairOfUidAndBookIds.length){
                    //6. 按照实际情况进行分配数组内存
                    pairOfUidAndBookIds = new PairOfUidAndBookId[allBorrowedBookLen];
                }
                //7. 把数据拷贝回到 已借阅图书信息的数组当中
                for (int i = 0; i < allBorrowedBookLen; i++) {
                    pairOfUidAndBookIds[i] = allBorrowedBook[i];
                }

                borrowedCount = allBorrowedBookLen;
            }

        }catch (IOException e){
            throw new RuntimeException(e);
        }
    }

    private void storeBorrowedBook(){
        try {
            analyzingBorrowedBook.storeBorrowedBook(pairOfUidAndBookIds, Constant.BORROWED_BOOK_FILE_NAME);

        }catch (IOException e){
            throw new RuntimeException(e);
        }
    }

    @Override
    public int display() {
        System.out.println("普通用户 " + getName() + " 的操作菜单:");
        System.out.println("1. 查找图书");
        System.out.println("2. 打印所有图书");
        System.out.println("3. 退出系统");
        System.out.println("4. 借阅图书");
        System.out.println("5. 归还图书");
        System.out.println("6. 查看当前个人借阅情况");
        System.out.println("请选择你的操作:");
        return scanner.nextInt();
    }

    // 借阅图书
    public void borrowBook(){

        scanner.nextLine();
        System.out.println("请输入你要借阅图书的id:");
        int bookId = scanner.nextInt();

        // 如果书架没有书 不能借阅
        if(library.bookCount == 0){
            System.out.println("书架没有书籍, 暂且不能借阅");
            return;
        }

        loadBorrowedBook();

        // 判断要借阅的书 是否是已经被借阅过了
        Book book = library.searchById(bookId);

        if(book == null){
            System.out.println("没有该id的相关书籍:");
        }
        for (int i = 0; i < borrowedCount; i++){
            PairOfUidAndBookId pairOfUidAndBookId = pairOfUidAndBookIds[i];
            if(pairOfUidAndBookId.getBookId() == book.getBookId()){
                if(getUserId() == pairOfUidAndBookId.getUserId()){
                    System.out.println("该书已经被你借阅过了,你的ID是:"+getUserId());
                    return;
                }else {
                    System.out.println("该书已经被其他人借阅了, 他的ID是:" + pairOfUidAndBookId.getUserId());
                    return;
                }
            }
        }

        library.borrowBook(bookId);

        PairOfUidAndBookId pairOfUidAndBookId = new PairOfUidAndBookId(getUserId(), book.getBookId());
        pairOfUidAndBookIds[borrowedCount] = pairOfUidAndBookId;
        borrowedCount++;

        // 存储借阅图书
        storeBorrowedBook();

        System.out.println("借阅成功");

    }

    // 归还图书
    public void returnBook(){
        loadBorrowedBook();

        if(borrowedCount == 0){
            System.out.println("目前没有用户借阅过书籍");
            return;
        }
        scanner.nextLine();
        System.out.println("请输入你要归还图书的id: ");
        int bookId = scanner.nextInt();
        scanner.nextLine();

        // 判断这本书是否存在图书馆
        Book book = library.searchById(bookId);
        if(book == null){
            System.out.println("没有该id的相关书籍: "+bookId);
            return;
        }

        // 判断要归还的书 是否已经被自己借阅过
        for (int i = 0; i < borrowedCount; i++) {
            if(pairOfUidAndBookIds[i].getBookId() == book.getBookId()){
                if(getUserId() == pairOfUidAndBookIds[i].getUserId()){
                    library.returnBook(bookId);
                    System.out.println("图书 " + book.getTitle() + "已成功归还。");
                    // 用最后一本书的借阅信息替换成
                    pairOfUidAndBookIds[i] = pairOfUidAndBookIds[borrowedCount-1];

                    // 清空最后一个
                    pairOfUidAndBookIds[borrowedCount-1] = null;

                    borrowedCount--;
                    storeBorrowedBook();

                }else {
                    System.out.println("该书籍不是你借阅的书籍, 不能归还:"+book.getTitle());
                }
                return;
            }
        }

        System.out.println("这本书没有借阅该书籍, 不需要归还, 书籍ID为"+bookId);
    }

    // 查看个人借阅情况
    public void viewBorrowHistory(){
        // 读取当前借阅所有用户的借阅书籍的情况
        loadBorrowedBook();
        System.out.println("您的借阅情况如下: ");
        if(borrowedCount == 0){
            System.out.println("目前没有借阅记录......");
        }else {
            boolean flg = false;
            for (int i = 0; i < borrowedCount; i++) {
                // 这里只能查看属于自己借阅情况
                // 用户ID相同的情况下, 使用书籍ID查询书籍
                if(pairOfUidAndBookIds[i].getUserId() == getUserId()){
                    flg = true;
                    Book book = library.searchById(pairOfUidAndBookIds[i].getBookId());
                    System.out.println(book);
                }
            }
            if(!flg){
                System.out.println("您没有借阅过书籍!");
            }
        }
    }
}
